package com.xhwl.data.util;

import com.xhwl.common.utils.CentDataVerifyUtil;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * @Author JCccc
 * @Description 版本号方法封装
 * @Date 2021/7/20 8:59
 */
public class VersionUtil {

    /**
     * true（new版本号大） 需要更新   false（new版本号小） 不需要更新
     * @param newVersion
     * @param currentVersion
     * @return
     */
    public static Boolean compareVersion(String newVersion,String currentVersion){
        // 先去除字母
        newVersion = newVersion.replaceAll("[a-zA-Z]","" );
        currentVersion= currentVersion.replaceAll("[a-zA-Z]","" );
        //切分版本字符串
        String[] v1s = newVersion.split("\\.");
        String[] v2s = currentVersion.split("\\.");
        //针对结果进行版本号大小判断
        int result = compareString(v1s,v2s);
        if(result>0){
           return true;
        }else if(result<0){
            return false;
        }else if(result == 0){
            return false;
        }
        return false;
    }

    public static int compareString(String[] v1s,String[] v2s){
        int len = 0;
        //得到两个数组长度的最大值，长度相等返回第二个
        if(v1s.length>v2s.length){
            len = v1s.length;
        }else{
            len = v2s.length;
        }
        //循环逐一比较两个数组相应位置的大小
        for(int i=0;i<len;i++){
            try {
                int com = compareChars(v1s[i], v2s[i]);
                if (com != 0) {
                    return com;
                }
            }catch (ArrayIndexOutOfBoundsException e){
                if(v1s.length>v2s.length){
                    return 1;
                }else{
                    return -1;
                }
            }
        }
        return 0;
    }

    /**
     * 把两个数组中相对应位置的元素拆分转成char类型进行比较
     * @param v1s
     * @param v2s
     * @return
     */
    public static int compareChars(String v1s,String v2s){
        char[] c1 = v1s.toCharArray();
        char[] c2 = v2s.toCharArray();

        //当两个数组长度一样时，1代表前者大，-1代表后者大
        if(c1.length==c2.length){
            for (int i = 0; i < c1.length; i++) {
                if(getHashCode(c1[i])>getHashCode(c2[i])){
                    return 1;
                }else if(getHashCode(c1[i])<getHashCode(c2[i])){
                    return -1;
                }
            }
        }

        //当后者长度大时，1代表前者大，-1代表后者大
        if(c1.length<c2.length){
            for (int i = 0; i < c1.length; i++) {
                if(getHashCode(c1[i])>getHashCode(c2[i])){
                    return 1;
                }else if(getHashCode(c1[i])<getHashCode(c2[i])){
                    return -1;
                }
            }
            return 1;
        }

        //当前者长度大时，1代表前者大，-1代表后者大
        if(c1.length>c2.length){
            for (int i = 0; i < c2.length; i++) {
                if(getHashCode(c1[i])>getHashCode(c2[i])){
                    return 1;
                }else if(getHashCode(c1[i])<getHashCode(c2[i])){
                    return 1;
                }
            }
            return -1;
        }

        return 0;
    }

    /**
     * 根据char计算出hash值进行比较
     * @param c
     * @return
     */
    public static int getHashCode(char c){
        return String.valueOf(c).toLowerCase().hashCode();
    }



}

